/*============================================================================
 * Project    : GameZone
 * Name       : NormalContainer.cpp
 * Author     : Administrator
 * Version    : 1.0
 * Copyright  : Copyright (c) 2009,Aspire-tech technology com, Ltd.
 * All rights reserved.
 * Created on : 2010-10-2 11:39:28
 * Description: {description}
 *============================================================================
 */

#include "NormalContainer.h"

CNormalContainer::CNormalContainer()
{
	iTitleBar = NULL;
    iContainer = NULL;
    iIndicator = NULL;
}

CNormalContainer::~CNormalContainer()
{
	delete iTitleBar;
	iTitleBar = NULL;
    delete iContainer;
    iContainer = NULL;
    delete iIndicator;
    iIndicator = NULL;
}

CNormalContainer* CNormalContainer::NewL( 
        const TRect& aRect, 
        const CCoeControl* aParent, 
        MEikCommandObserver* aCommandObserver )
    {
    CNormalContainer* self = CNormalContainer::NewLC( 
            aRect, 
            aParent, 
            aCommandObserver );
    CleanupStack::Pop( self );
    return self;
    }

CNormalContainer* CNormalContainer::NewLC( 
        const TRect& aRect, 
        const CCoeControl* aParent, 
        MEikCommandObserver* aCommandObserver )
    {
    CNormalContainer* self = new ( ELeave ) CNormalContainer();
    CleanupStack::PushL( self );
    self->ConstructL( aRect, aParent, aCommandObserver );
    return self;
    }
            
void CNormalContainer::ConstructL( 
        const TRect& aRect, 
        const CCoeControl* aParent, 
        MEikCommandObserver* aCommandObserver )
    {
    if ( aParent == NULL )
        {
        CreateWindowL();
        }
    else
        {
        SetContainerWindowL( *aParent );
        }
    iCommandObserver = aCommandObserver;
    iFocusControl = NULL;
    InitializeControlsL(aRect);
    SetRect( aRect );
    ActivateL();
    }

TInt CNormalContainer::CountComponentControls() const
    {
    return ( int ) ELastControl;
    }
                
CCoeControl* CNormalContainer::ComponentControl( TInt aIndex ) const
    {
    switch ( aIndex )
        {
    	case ETitleBar:
    		return iTitleBar;
        case EContainer:
            return iContainer;
        case EIndicator:
        	return iIndicator;
        }
    return NULL;
    }
                            
void CNormalContainer::SizeChanged()
    {
    CCoeControl::SizeChanged();
    LayoutControls();
    }

void CNormalContainer::LayoutControls()
	{
	TRect rect = Rect();
	rect.SetHeight(44);
	iTitleBar->SetExtent(TPoint(0, 0),rect.Size());
	rect.SetHeight(Rect().Height() - 44);
    iContainer->SetExtent(TPoint(0, 44), rect.Size());
    iIndicator->SetRect(iIndicator->Rect());
	}
         
TKeyResponse CNormalContainer::OfferKeyEventL( 
        const TKeyEvent& aKeyEvent, 
        TEventCode aType )
    {
    if ( iFocusControl != NULL
        && iFocusControl->OfferKeyEventL( aKeyEvent, aType ) == EKeyWasConsumed )
        {
        return EKeyWasConsumed;
        }
    return CCoeControl::OfferKeyEventL( aKeyEvent, aType );
    }
                        
void CNormalContainer::InitializeControlsL(const TRect& aRect)
    {
	TRect rect = aRect;
	rect.SetHeight(44);
	iTitleBar = CTitleBar::NewL(rect,this);
	iTitleBar->SetEventObserver(this);
	rect.iTl.iY = 44;
	rect.iBr = aRect.iBr;
    iContainer = CCustomContainer::NewL(rect,this);
    iContainer->setEnableSkin(EFalse);
    iContainer->SetFocus( ETrue );
    iFocusControl = iContainer;
    
	iIndicator = CIndicatorControl::NewL(aRect,this);
	iIndicator->setIndicatorObserver(this);
	iIndicator->setLabelTextL(_L("wait..."));
	iIndicator->setCanSure(ETrue);
	iIndicator->setCanCancel(ETrue);
	//iIndicator->SetLayout();
	iIndicator->MakeVisible(EFalse);
    }

void CNormalContainer::HandleResourceChange( TInt aType )
    {
    CCoeControl::HandleResourceChange( aType );
    SetExtentToWholeScreen();
    }

void CNormalContainer::HandleControlEventL( CCoeControl* /*aControl*/, TEventType /*aEventType*/)
{

}

void CNormalContainer::HandleIndicatorEventL(TInt aResult)
	{
	
	}

void CNormalContainer::AddControlL(CCoeControl* aControl)
{
    iContainer->AddControlL(aControl);
}

void CNormalContainer::UpdateControls(TInt aOffset)
{
    iContainer->UpdateControls(aOffset);
}

void CNormalContainer::ShowWaitIndicator(const TRect& aRect, const TDesC& aText,TBool aCanCancel)
{
	if (iIndicator)
	{
	iIndicator->SetRect(aRect);
	iIndicator->setLabelTextL(aText);
	iIndicator->setCanCancel(aCanCancel);
	iIndicator->MakeVisible(ETrue);
	}
}

void CNormalContainer::HideWaitIndicator()
	{
	if (iIndicator)
		{
		iIndicator->MakeVisible(EFalse);
		}
	}

void CNormalContainer::HandlePointerEventL(const TPointerEvent& aPointerEvent)
{
	if (iIndicator && iIndicator->IsVisible())
		{
		iIndicator->HandlePointerEventL(aPointerEvent);
		}
	else
		{
		TRect Rect;
		if (iTitleBar)
			{
			Rect = iTitleBar->Rect();
			if (Rect.Contains(aPointerEvent.iPosition))
				{
				iTitleBar->HandlePointerEventL(aPointerEvent);
				}
			}
		if (iContainer)
			{
			Rect = iContainer->Rect();
			if (Rect.Contains(aPointerEvent.iPosition))
				{
				if (iContainer != iFocusControl)
					iContainer->SetFocus(ETrue);
				iContainer->HandlePointerEventL(aPointerEvent);
				DrawNow();
				}
			}
		}
}

void CNormalContainer::setTitleTextL(const TDesC& aText)
	{
	if (iTitleBar)
		iTitleBar->SetCaptionL(aText);
	}

void CNormalContainer::setTitleButton(const TDesC& aText, TInt aActionType)
	{
	if (iTitleBar)
		iTitleBar->setButtonTextL(aText,aActionType);
	}
